package com.ysm.smfu.food;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.umeng.socialize.shareboard.ShareBoardConfig;
import com.umeng.socialize.shareboard.SnsPlatform;
import com.umeng.socialize.utils.ShareBoardlistener;
import com.ysm.smfu.R;
import com.ysm.smfu.StatusBarCompat;
import com.ysm.smfu.activity.BaseActivity;
import com.ysm.smfu.contans.Defaultcontent;
import com.ysm.smfu.contans.SpKey;
import com.ysm.smfu.food.share.CustomShareListener;
import com.ysm.smfu.food.share.ShareBuild;
import com.ysm.smfu.food.view.FoodDetailView;
import com.ysm.smfu.map.BasicMapActivity;
import com.ysm.smfu.utils.UIUtils;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FoodDetailActivity extends BaseActivity implements FoodDetailView {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.share)
    ImageView share;
    @BindView(R.id.moreLargeImg)
    AutoRelativeLayout moreLargeImg;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.currLoc)
    TextView currLoc;
    @BindView(R.id.phone)
    ImageView phone;
    @BindView(R.id.aa)
    TextView aa;
    @BindView(R.id.ab)
    View ab;
    @BindView(R.id.img)
    ImageView img;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.line)
    View line;
    @BindView(R.id.timeFilter)
    TextView timeFilter;
    @BindView(R.id.sec)
    AutoLinearLayout sec;
    @BindView(R.id.profilePrice)
    TextView profilePrice;
    @BindView(R.id.normalPrice)
    TextView normalPrice;
    @BindView(R.id.aleadySold)
    TextView aleadySold;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.count)
    TextView count;
    @BindView(R.id.wifi)
    TextView wifi;
    private PopupWindow mPopupWindow;
    private View callPhoneView;
    private TextView phoneNum;
    private TextView cancel;
    private ShareAction mShareAction;
    private CustomShareListener mShareListener;

    @Override
    protected void initViews() {
        super.initViews();
        setContentView(R.layout.activity_food_detail);
        callPhoneView = LayoutInflater.from(this).inflate(R.layout.callphone, null);
        ButterKnife.bind(this);
        StatusBarCompat.compat(FoodDetailActivity.this, getResources().getColor(R.color.green));
        setTopBar(back, 0);
        setTopBar(share, 10);
    }

    /**
     * 设置适应状态栏
     *
     * @param height 距离顶部的附加高度
     */
    private void setTopBar(View view, int height) {
        AutoRelativeLayout.LayoutParams layoutParams = (AutoRelativeLayout.LayoutParams) view.getLayoutParams();
        layoutParams.topMargin = StatusBarCompat.getStatusBarHeight(this) + height;
        view.setLayoutParams(layoutParams);
    }

    /**
     * 展示数据
     */
    @Override
    public void showData() {
    }

    @OnClick({R.id.moreLargeImg, R.id.phone, R.id.back, R.id.share, R.id.goMap})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.moreLargeImg:
                goPhoto();
                break;
            case R.id.phone:
                showPhone();
                break;
            case R.id.cancel:
                cancelCallPhone();
                break;
            case R.id.phoneNum:
                onCall(SpKey.SEAVERNUMBER);
                break;
            case R.id.back:
                finish();
                break;
            case R.id.share:
                sharePage();
                break;
            case R.id.goMap:
                UIUtils.startActivity(BasicMapActivity.class, "singal", true);
                break;
        }
    }

    /**
     * 分享
     */
    private void sharePage() {
        if (mShareListener == null)
            mShareListener = new CustomShareListener(activity);
        if (mShareAction == null)
            mShareAction = ShareBuild.getShareAction(this, mShareListener);
        mShareAction.open(ShareBuild.getShareBoardConfig());
    }

    /**
     * 展示打电话页面
     */
    private void showPhone() {
        if (mPopupWindow == null) {
            mPopupWindow = new PopupWindow(callPhoneView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            mPopupWindow.setAnimationStyle(R.style.PopupWindowAnimation);
            mPopupWindow.setFocusable(false);
            mPopupWindow.setOutsideTouchable(true);
            phoneNum = (TextView) callPhoneView.findViewById(R.id.phoneNum);
            phoneNum.setText(SpKey.SEAVERNUMBER);
            cancel = (TextView) callPhoneView.findViewById(R.id.cancel);
            phoneNum.setOnClickListener(this);
            cancel.setOnClickListener(this);
        }
        mPopupWindow.showAtLocation(phone, Gravity.CENTER, 0, 0);
    }

    /**
     * 隐藏打电话页面
     */
    public void cancelCallPhone() {
        if (mPopupWindow != null && mPopupWindow.isShowing())
            mPopupWindow.dismiss();
    }

    @Override
    public void onBackPressed() {
        if (mPopupWindow != null && mPopupWindow.isShowing())
            mPopupWindow.dismiss();
        else
            finish();
    }

    /**
     * 跳往商家相册
     */
    private void goPhoto() {
        UIUtils.startActivity(FoodPhotoActivity.class);
    }

    /**
     * 打电话
     *
     * @param mobile
     */
    public void onCall(String mobile) {
        if (Build.VERSION.SDK_INT >= 23) {
            int checkCallPhonePermission = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE);
            if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 100);
                return;
            } else {
                //上面已经写好的拨号方法
                callDirectly(mobile);
            }
        } else {
            //上面已经写好的拨号方法
            callDirectly(mobile);
        }
    }

    private void callDirectly(String mobile) {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.CALL");
        intent.setData(Uri.parse("tel:" + mobile));
        startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 屏幕横竖屏切换时避免出现window leak的问题
     */
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mShareAction.close();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
